package com.flyco.modules.stm.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.mapper.OmsOrderMapper;
import com.flyco.modules.stm.entity.StmDealerDiscount;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.mapper.StmDealerDiscountMapper;
import com.flyco.modules.stm.model.DiscountApproveDTO;
import com.flyco.modules.stm.service.IStmDealerDiscountService;
import com.flyco.modules.task.mapper.ToSapConfigMapper;
import com.flyco.modules.task.service.SapTaskService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 经销商折扣表
 * @Author: flyco
 * @Date: 2024-04-11
 * @Version: V1.0
 */
@Service
public class StmDealerDiscountServiceImpl extends ServiceImpl<StmDealerDiscountMapper, StmDealerDiscount> implements IStmDealerDiscountService {

    @Autowired
    private StmDealerDiscountMapper stmDealerDiscountMapper;
    @Resource
    private SapTaskService sapTaskService;

    @Override
    @Transactional
    public void saveMain(StmDealerDiscount stmDealerDiscount) {
        stmDealerDiscountMapper.insert(stmDealerDiscount);
    }

    @Override
    @Transactional
    public void updateMain(StmDealerDiscount stmDealerDiscount) {
        stmDealerDiscountMapper.updateById(stmDealerDiscount);
    }

    @Override
    @Transactional
    public void delMain(String id) {
        stmDealerDiscountMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            stmDealerDiscountMapper.deleteById(id);
        }
    }

    @Override
    @Transactional
    public void auditBatch(DiscountApproveDTO approveDTO) {
        List<Long> idList = Arrays.asList(approveDTO.getIds().split(",")).stream().map(o -> {
            Long id = Long.valueOf(o);
            return id;
        }).collect(Collectors.toList());

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        for (Long id : idList) {
            StmDealerDiscount stmDealerDiscount = stmDealerDiscountMapper.selectById(id);
            stmDealerDiscount.setAuditStatus(approveDTO.getAuditType());
            stmDealerDiscount.setAuditTime(DateTime.now());
            stmDealerDiscount.setAuditBy(sysUser.getUsername());
            stmDealerDiscount.setNote(approveDTO.getNote());
            stmDealerDiscountMapper.updateById(stmDealerDiscount);
        }
    }

    @Override
    @Transactional
    public void updateBatch(List<StmDealerDiscount> stmDealerDiscountList) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        for (StmDealerDiscount dealerDiscount : stmDealerDiscountList) {
            StmDealerDiscount stmDealerDiscount = stmDealerDiscountMapper.selectById(dealerDiscount.getId());
            stmDealerDiscount.setProductAmount(dealerDiscount.getProductAmount());
            stmDealerDiscount.setMissionAmount(dealerDiscount.getMissionAmount());
            stmDealerDiscount.setPromotionAmount(dealerDiscount.getPromotionAmount());
            stmDealerDiscount.setAdjustAmount(dealerDiscount.getAdjustAmount());
            stmDealerDiscount.setAuditStatus(0);
            stmDealerDiscount.setUpdateBy(sysUser.getUsername());
            stmDealerDiscount.setUpdateTime(DateTime.now());
            stmDealerDiscountMapper.updateById(stmDealerDiscount);
        }
    }

    @Override
    public StmDealerDiscount getDiscountByStoreId(StmStoreInfo storeInfo) {
        //更新折扣额度 by martin 2024-06-26
        sapTaskService.updateDealerDiscount(storeInfo.getPartnerId(), storeInfo.getStoreId().toString());
        LambdaQueryWrapper<StmDealerDiscount> query = new LambdaQueryWrapper();
        query.eq(StmDealerDiscount::getStoreId, storeInfo.getCompanyCode())
                .eq(StmDealerDiscount::getCompanyCode, storeInfo.getPartnerId())
                .eq(StmDealerDiscount::getChannelCode, storeInfo.getChannelId())
                .eq(StmDealerDiscount::getAuditStatus, 1);
        List<StmDealerDiscount> dealerDiscountList = stmDealerDiscountMapper.selectList(query);
        if (!CollectionUtils.isEmpty(dealerDiscountList)) {
            return dealerDiscountList.get(0);
        }
        StmDealerDiscount stmDealerDiscount = new StmDealerDiscount();
        BigDecimal remainAmount = BigDecimal.ZERO;
        stmDealerDiscount.setProductRemainAmount(remainAmount);
        stmDealerDiscount.setMissionRemainAmount(remainAmount);
        stmDealerDiscount.setPromotionRemainAmount(remainAmount);
        stmDealerDiscount.setAdjustRemainAmount(remainAmount);
        return stmDealerDiscount;
    }

    @Override
    public List<StmDealerDiscount> getDiscountByStoreIdAndTime(String storeId, Date startTime, Date endTime, String companyCode, String channelCode) {
        LambdaQueryWrapper<StmDealerDiscount> query = new LambdaQueryWrapper();
        query.eq(StmDealerDiscount::getStoreId, storeId)
                .le(StmDealerDiscount::getStartTime, DateTime.now())
                .ge(StmDealerDiscount::getEndTime, DateTime.now())
                .eq(StmDealerDiscount::getCompanyCode, companyCode)
                .eq(StmDealerDiscount::getChannelCode, channelCode)
                .in(StmDealerDiscount::getAuditStatus, 0, 1);
        List<StmDealerDiscount> dealerDiscountList = stmDealerDiscountMapper.selectList(query);
        return dealerDiscountList;
    }

}
